<?php
if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}

    $objXajax->registerFunction('fmfrmcharttimerent_viewchart'); 

    function fmfrmcharttimerent_viewchart($charttype=1,$dateFrom='',$dateTo=''){
    
        global $objDbSelect,$arrTitle;
        $arrTitle = array(
            1=>'Thời gian thuê theo ngày(giờ)',
            2=>'Thời gian thuê theo phòng(giờ)',
            3=>'Thời gian thuê trung bình theo ngày trong tuần(giờ)',
            4=>'Tiền chi'
         );
        $objResponse = new xajaxResponse();
        include(ROOT_PATH.'adagps.com/Charts/ofc/php-ofc-library/open-flash-chart.php');    
        $dateFrom = $dateFrom==''?SYSTEM_DATE_START  : $dateFrom;
        $dateTo = $dateTo==''?SYSTEM_DATE  : $dateTo;
        $arrResult = fmfrmcharttimerent_createArray($charttype,$dateFrom,$dateTo);
        
        $arrName = Array(); 
        $max = 5;
        $title = new title( $arrTitle[$charttype],20) ;
        $format = 2;
        $sName='';
        $max =0;
        $min =0;
        $chart = new open_flash_chart();
        $barLabel = array();
        $barArr = array();
        $bar =0;
        
        if($charttype==1){
            for($i=0;$i<count($arrResult);$i++){
                $sName =  $arrResult[$i]['name'];
                $xx = new x_axis_label($sName, '#050069', 13, 340);            
                $barLabel[] = $xx;
                $dec = (int)$arrResult[$i]['total'];
                if($dec>$max)$max=$dec;
                if($dec<$min)$min=$dec;
                $d = new dot($dec);               
                $barArr[] = $d->colour('#D02020')->tooltip($sName."<br>".$dec.'h');  
               
              }
              $bar = new line();
              $bar->set_tooltip('Value = #val');
              $bar->set_on_show(new line_on_show('grow-up', 2.5, 0));
              $bar->set_values($barArr); 
              
             
        }else if($charttype==2){
            $barArr = array();
            for($i=0;$i<count($arrResult);$i++){
                $sName =  $arrResult[$i]['name'];
                $xx = new x_axis_label($sName, '#050069', 13, 340);            
                $barLabel[] = $xx;
                $dec = (int) $arrResult[$i]['total'];
                $barArr[] = $dec; 
                if($dec>$max)$max=$dec;
                if($dec<$min)$min=$dec;

            }
           // die();
            $bar = new bar_glass();
            $bar->set_colour('#68D5ED');
            $bar->set_values($barArr);
            $bar->key($arrTitle[$charttype],12); 
            
        }
        else if($charttype==3){
            $barArr = array();
            for($i=0;$i<count($arrResult);$i++){
                $sName =  $arrResult[$i]['name'];
                $dec = $arrResult[$i]['total'];
                array_push($barArr,new pie_value($dec,addslashes("$sName")));

            }
            $bar = new pie();  
            $bar->set_alpha(0.8);
            $bar->set_start_angle( 35 );
            $bar->add_animation( new pie_fade() )->add_animation( new pie_bounce(10) ) ; 
            $bar->set_tooltip( '#val#h <br> #percent# ' );
            $bar->set_colours(array('#1C9E05','#FF368D','#0000FF','#06DCFB','#FF800D','#B96F6F','#06DCFB'));
            $bar->set_values( $barArr );

        }
        if($charttype!=3){
            $rangeMax =0;
            $rangeMin =0;
            $rangStep=1;
            createRangeForChart($min,$max,$rangeMax,$rangeMin);
            $y = new y_axis();
            $y->set_range($rangeMin,$rangeMax);
            $y->set_steps(($rangeMax-$rangeMin)/5);
            
            $x_labels = new x_axis_labels();
            $x = new x_axis();
            $x_labels->set_labels($barLabel );
            $x->set_labels($x_labels);
               
            $chart->set_x_axis($x);
            $chart->set_y_axis($y);
            
        }
        
        

        $chart->add_element($bar);
        $chart->set_title($title);        
        $values = $chart->toPrettyString();
        
       $sScript = 'data_fmcharttimerent=' . $values .';';                
        
       $objResponse->addScript($sScript);    
        $sScript1 = 'swfobject.embedSWF("adagps.com/Charts/ofc/open-flash-chart.swf", "fmfrmcharttimerent_divGrid", "75%", "450", "9.0.0", "expressInstall.swf", {"get-data":"open_flash_chart_data_fmcharttimerent"}, {"wmode":"opaque"});';
        $objResponse->addScript($sScript1);  
        $objResponse->addScript("changeWaiting(1);");  
                          
        return $objResponse->getXML();    
    }
    
function fmfrmcharttimerent_createArray($charttype,$dateFrom,$dateTo){
    global $objDbUpdate,$tbl_postfix,$hotel_id;
   
   $sql='';
   switch($charttype){
     case 1:$sql=" select DATE_FORMAT(h.timeend,'%d/%m/%Y') as name,sum(TIMESTAMPDIFF(MINUTE,h.timestart,h.timeend))
                   as total from fm_current_book$tbl_postfix as h where datediff('$dateFrom',h.timeend)<=0 and
                   datediff('$dateTo',h.timeend)>=0 group by DATE_FORMAT(h.timeend,'%Y-%m-%d') and h.flg_cancel = 0 order by  DATE_FORMAT(h.timeend,'%Y-%m-%d')";
     break;  
     case 2:$sql=" select r.name as name ,sum(TIMESTAMPDIFF(MINUTE,h.timestart,h.timeend))
                   as total from fm_current_book$tbl_postfix as h join fm_room r ON h.room_id = r.id where datediff('$dateFrom',h.timeend)<=0 and
                   datediff('$dateTo',h.timeend)>=0 and h.flg_cancel = 0 group by h.room_id order by  h.room_id ";break; 
                    
     case 3:$sql="select DAYOFWEEK(h.date_sum) as dayweek,count(DAYOFWEEK(h.date_sum)) as numdw ,sum(timerent)
                   as total from fm_summary as h  where datediff('$dateFrom',h.date_sum)<=0 and
                   datediff('$dateTo',h.date_sum)>=0 and service_id = $hotel_id group by DAYOFWEEK(h.date_sum) order by  DAYOFWEEK(h.date_sum)";
                   break;
                   
                   
   }

    // echo $sql; die();  
     $arr = $objDbUpdate->GetArray($sql);
     $count = count($arr);
     for($i=0;$i<$count;$i++){ //convertToNameDayOfWeek($arrResult[$i]['dayweek'])
         
         if($charttype==3){
           $arr[$i]['total']=(int)$arr[$i]['total']/$arr[$i]['numdw'];
           $arr[$i]['name']=convertToNameDayOfWeek($arr[$i]['dayweek']);  
         }
         $arr[$i]['title']=ConvertTimeToHM($arr[$i]['total']);
         $arr[$i]['total']= round($arr[$i]['total']/60,1);
          
     } 
    // echo '<pre>'; print_r($arr); die();
     return  $arr  ;
} 


   
?>